import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from "../components/Login";
import Main from "../components/main/Main";
import Welcome from "../components/Welcome";
import User from "../components/user/User";
import Goods from "../components/goods/Goods";
import Manger from "../components/show/Manger";
import Progress from "../components/progress/Progress";

Vue.use(VueRouter)

const routes = [
  {path:'/',redirect:'/login'},
  {path:'/login',name:"login",component:Login},
  {path:'/main',name:"main",component:Main,redirect: '/welcome',
  children:[
    {path:'/welcome',name:"welcome",component:Welcome},
    {path:'/user',name:"user",component:User},
    {path:'/goods',name:"goods",component:Goods},
    {path:'/manager',name:"manager",component:Manger},
    {path: '/progress',name:'progress',component: Progress}
  ]},

]

const router = new VueRouter({
  routes
})
//导航守卫
router.beforeEach((to,from,next)=>{
  if (to.path==='/main'){
    const token = localStorage.getItem("token");
    //判断是否有token值
    if (token){
      next()
    }else {
      next('/login')
    }
  }else {
    next()
  }
})
export default router
